import LocalStorageUtil from '@/utils/local-storage-util';
import Vue from 'vue';
import _ from "lodash";

export default class Directive{
	//自定义指令（权限控制）
	public static auth() {
		let authList = [];
		return Vue.directive('auth', {
			inserted: function (el, binding) {
				const token = LocalStorageUtil.getObjectItem('userInfo');
				if (!_.isEmpty(token)) {
					authList = (authList.length > 0 ? authList : token.authorities.map(item => item.authority))
				}
				const {value} = binding;
				if (typeof value == 'string') {
					if (!_.isEmpty(value) && !authList.includes(value)) {
						el.style.display = 'none'
					}
				} else {
					let opt = false;
					value.map(item => {
						if (authList.includes(item)) {
							opt = true;
						}
					})
					if (!opt) {
						el.style.display = 'none';
					}
				}
			}
		});
	}
}
